package com.book.shop.controller;

import com.book.shop.Webservice.BookService;
import com.book.shop.Webservice.OrderService;
import com.book.shop.Webservice.UserService;
import com.book.shop.model.Book;
import com.book.shop.model.Order;
import com.book.shop.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    //查询订单信息
    @GetMapping("/order")
    public String getOrder(Model model){
        model.addAttribute("order",orderService.getOrder());
        return "order/order";
    }
    //查询历史订单信息
    @GetMapping("/historyOrder")
    public String getHistoryOrder(Principal principal,Model model){
        List<Order> orders = orderService.findByUserName(principal.getName());
        model.addAttribute("orders",orders);
        return "order/historyOrder";
    }
    //订单完成
    @GetMapping("/confirmOrder")
    public String showOrderConfirmInfo(Principal principal,Model model){
        Order orders = orderService.clearOrder();
        model.addAttribute("orders",orders);
        return "order/orderComplete";
    }
    //新增订单
    @GetMapping("/order/createOrder")
    public String createOrder(Principal principal,Model model){
       if(orderService.getOrder().getBooks().isEmpty()){
           model.addAttribute("errorMessage","订单为空");
           return getOrder(model);
       }
       orderService.createOrder(principal.getName());
       model.addAttribute("order",orderService.getOrder());
       User user = userService.findByUserName(principal.getName());
       model.addAttribute("user",user);
       return "order/checkout";
    }
    //添加图书到订单
    @GetMapping("/order/addBook/{bookId}")
    public String addBookToOrder(@PathVariable("bookId") Long bookId,Model model){
        orderService.AddBookToOrder(bookService.findBookById(bookId));
        return getOrder(model);
    }
    //从订单中删除图书
    @GetMapping("/order/removeBook/{bookId}")
    public String removeBookFromOrder(@PathVariable("bookId") Long bookId,Model model){
        Book bookDel = bookService.findBookById(bookId);
        orderService.removeBookFromOrder(bookDel);
        return getOrder(model);
    }

    //查询所有订单信息
    @GetMapping("/orders/all")
    @Secured("ROLE_ADMIN")
    public String getAllOrders(Model model){
        final List<Order> orders = orderService.findAllOrders();
        model.addAttribute("orders",orders);
        return "order/showAllOrders";
    }

    //删除订单信息
    @GetMapping("/orders/delete/{orderId}")
    @Secured("ROLE_ADMIN")
    public String deleteOrder(@PathVariable("orderId") Long orderId,Model model ){
        Order order = orderService.findOrderById(orderId);
        orderService.deleteOrder(order);
        model.addAttribute("successMessage","订单编号"+orderId+"删除成功");
        return  getAllOrders(model);
    }

    //查询指定订单信息
    @GetMapping("/order/{orderId}")
    public String showOrderItem(@PathVariable Long orderId,Model model){
        Order order = orderService.findOrderById(orderId);
        model.addAttribute("order",order);
        return "order/showOrderItem";
    }
    //订单完成
    @GetMapping("/orders/complete/{orderId}")
    public String completeOrderItem(@PathVariable Long orderId,Model model){
        final Order order = orderService.findOrderById(orderId);
        order.setOrderStatus(true);
        orderService.updateOrder(order);
        return getAllOrders(model);
    }

}
